bin/orphaned-pr-environments: Handle no PR envs and merged PRs better#889
Merged
bin/orphaned-pr-environments: Handle no PR envs and merged PRs better#889
Conversation
lorenyu
approved these changes
Mar 3, 2025
Contributor
lorenyu
left a comment
There was a problem hiding this comment.
I had a question on the advantage of test $? = 1 over || true but I trust your expertise on bash over mine so gonna approve. I also had a question regarding the MERGED status, I have never actually seen that status
| echo terraform -chdir="infra/${app_name}/service" workspace list | ||
| workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)" | ||
| pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' | sed 's/p-//')" | ||
| pr_nums="$(echo "${workspaces}" | { grep -o 'p-[0-9]\+' || test $? = 1; } | sed 's/p-//')" |
Contributor
There was a problem hiding this comment.
can we add a comment like
# Search for workspaces that start with "p-" and extract the PR numbers
# The "test $? = 1" is used to avoid errors if there are no PR environments
also, can you help me understand the advantage of test $? = 1 over || true? The latter seems simpler to grok.
Contributor
Author
There was a problem hiding this comment.
|| true ignores all errors, which while unlikely seems worse than just ignoring the specific expected case. grep will exit with 2 if something else is wrong.
Contributor
There was a problem hiding this comment.
ah ok that makes sense, thanks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
When there are no PR environments for an application,
grep 'pr-'against theworkspace names will not match anything, in which case grep returns a code of
1. Since-o pipefailis set,grepreturning1will fail the pipeline,and since
-eis set, the pipeline will fail the script. So catch a return of1fromgrepand continue on.Also handle merged PR statuses in addition to closed, as that will sometimes be
the case.
Also set a default for
GITHUB_STEP_SUMMARYfor when running the scriptlocally.
Testing
Before
Errors out when there are no PR envs.
After
Does not error out.
Before
Doesn't flag dangling PR environment from a merged PR.
After